package com.yc.UI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.utils.SWTUtil;

/**
 *模板
 * @author UNO
 * 2021年6月20日下午3:45:46
 */
public class TempUI {

	protected Shell shell;
	private boolean down = false;
	private int clickX = 0;
	private int clickY = 0;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TempUI window = new TempUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NONE);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(905, 507);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		//背景图片
		composite.setBackgroundImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/back.jpg"));
		// 设置窗口居中
		SWTUtil.centerWindom(shell);

		
		//设置图标
		Label label_Icon = new Label(composite, SWT.NONE);
		label_Icon.setBounds(0, 0, 40, 40);
		label_Icon.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/福字.png"));
		
		//关闭按钮
		Label label_close = new Label(composite, SWT.NONE);
		
		label_close.setBounds(863, 0, 40, 40);
		label_close.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/关闭.png"));
		
		//最小化按钮
		Label label_min = new Label(composite, SWT.NONE);
		label_min.setBounds(817, 0, 40, 40);
		label_min.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/最小化.png"));
		
		Label label = new Label(composite, SWT.CENTER);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BORDER));
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.BOLD));
		label.setBounds(288, 127, 251, 40);
		label.setText("欢迎使用12306");
		
		//关闭按钮操作
		label_close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.dispose();
				System.exit(0);
			}
		});
		//最小化操作
		label_min.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		//面板拖动
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				down = true;
				clickX = e.x;
				clickY = e.y;
			}
			@Override
			public void mouseUp(MouseEvent e) {
				down = false;
			}
		});
		
		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if(down) {  //down为true，说明鼠标按下，窗口跟着鼠标移动			
					shell.setLocation(shell.getLocation().x + e.x - clickX, shell.getLocation().y + e.y - clickY);
				}
				
			}
		});

	}

}
